<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>$.jql - unit test</title>
<link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css" type="text/css" media="screen" />
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://code.jquery.com/qunit/git/qunit.js"></script>
<script type="text/javascript" src="../jquery.logger.js"></script>
<script>
$(document).ready( function() {
    module('Core');
    
    test('Test initial value', function() {
        expect(2);
        
        $.jql.reset();
        var options = $.jql.getOptions();
        ok((options.level == $.jql.INFO), 'Default level is info.' );
        var defaultAppender = options.appender;
        ok((defaultAppender != null && defaultAppender.append != null), 'Default appender contains append() method.');
    });
    
    test('$.jql.setName() and $.jql.getName()', function() {
        expect(1);
        
        $.jql.reset();
        
        var name = 'myLoggerName';
        $.jql.setName(name);
        
        equals($.jql.getName(), name, 'Name is set correctly.');
    });
    
    test('$.jql.setLevel() and $.jql.getLevel()', function() {
        expect(6);
        
        $.jql.reset();
        
        $.jql.setLevel($.jql.TRACE);
        equals($.jql.getLevel(), $.jql.TRACE, 'Set level to TRACE and get level returns TRACE.' );
        
        $.jql.setLevel($.jql.DEBUG);
        equals($.jql.getLevel(), $.jql.DEBUG, 'Set level to DEBUG and get level returns DEBUG.' );
        
        $.jql.setLevel($.jql.INFO);
        equals($.jql.getLevel(), $.jql.INFO, 'Set level to INFO and get level returns INFO.' );
        
        $.jql.setLevel($.jql.WARN);
        equals($.jql.getLevel(), $.jql.WARN, 'Set level to WARN and get level returns WARN.' );
        
        $.jql.setLevel($.jql.ERROR);
        equals($.jql.getLevel(), $.jql.ERROR, 'Set level to ERROR and get level returns ERROR.' );
        
        $.jql.setLevel($.jql.FATAL);
        equals($.jql.getLevel(), $.jql.FATAL, 'Set level to FATAL and get level returns FATAL.' );
    });

    test('$.jql.setAppender() and $.jql.getAppender()', function() {
        expect(1);
        
        $.jql.reset();
        
        var fakeAppender = function() {
            return {
                'append' : function() {
                    return Number.MAX_VALUE;
                }
            };
        };
        $.jql.setAppender(fakeAppender());
        var appender = $.jql.getAppender();
        ok((appender.append() == Number.MAX_VALUE), 'Appender is set correctly.');
    });

    test('$.jql.setOptions() and $.jql.getOptions()', function() {
        expect(2);
        
        $.jql.reset();
        var fakeAppender = function() {
            return {
                'append' : function() {
                    return Number.MAX_VALUE;
                }
            };
        };
        $.jql.setOptions({
            'level' : $.jql.ERROR,
            'appender' : fakeAppender()
        });
        var options = $.jql.getOptions();
        ok((options.level == $.jql.ERROR), 'Level is set to ERROR.' );
        ok((options.appender.append() == Number.MAX_VALUE), 'Appender is set correctly.');
    });
    
    module('Local Appender');
    
    test('logging methods - $.sql.trace(), $.sql.debug(), $.sql.info(), $.sql.warn(), $.sql.error(), $.sql.fatal()', function() {
        expect(36);
        
        $.jql.reset();
        
        var levels = {
            'trace' : [$.jql.TRACE, 'debug'], 
            'debug' : [$.jql.DEBUG, 'debug'], 
            'info' : [$.jql.INFO, 'info'], 
            'warn' : [$.jql.WARN, 'warn'], 
            'error' : [$.jql.ERROR, 'error'], 
            'fatal' : [$.jql.FATAL, 'error']
        };
        
        var result = null;
        console = {
            'debug' : function() { result = 'debug'; },
            'info' : function() { result = 'info'; },
            'warn' : function() { result = 'warn'; },
            'error' : function() { result = 'error'; },
        };
        
        for (var currentLevel in levels) {
            $.jql.setLevel($.jql[currentLevel.toUpperCase()]);
            
            for (var level in levels) {
                var levelId = levels[level][0];
                var expected = (levelId >= $.jql.getLevel()) ? levels[level][1] : null;
                
                result = null;
                $.jql[level].call($.jql);
                equals(result, expected, '$.jql.' + level + '() returns ' + expected + ' (level is ' + $.jql.getLevel() + ').');
            }
        }
    });
    
    module('$.jql.getLogger()');
    
    var defaultLoggerName = 'newLogger';
    var $logger = $.jql.getLogger(defaultLoggerName);
    
    test('Test initial value', function() {
        expect(3);
        
        $logger.reset();
        var options = $logger.getOptions();
        ok((options.level == $logger.INFO), 'Default level is info.' );
        var defaultAppender = options.appender;
        ok((defaultAppender != null && defaultAppender.append != null), 'Default appender contains append() method.');
        equals($logger.getName(), defaultLoggerName, 'Name is set correctly.');
    });
    
    test('$logger.setName() and $logger.getName()', function() {
        expect(1);
        
        $logger.reset();
        
        var name = 'myLoggerName';
        $logger.setName(name);
        
        equals($logger.getName(), name, 'Name is set correctly.');
    });
    
    test('$logger.setLevel() and $logger.getLevel()', function() {
        expect(6);
        
        $logger.reset();
        
        $logger.setLevel($logger.TRACE);
        equals($logger.getLevel(), $logger.TRACE, 'Set level to TRACE and get level returns TRACE.' );
        
        $logger.setLevel($logger.DEBUG);
        equals($logger.getLevel(), $logger.DEBUG, 'Set level to DEBUG and get level returns DEBUG.' );
        
        $logger.setLevel($logger.INFO);
        equals($logger.getLevel(), $logger.INFO, 'Set level to INFO and get level returns INFO.' );
        
        $logger.setLevel($logger.WARN);
        equals($logger.getLevel(), $logger.WARN, 'Set level to WARN and get level returns WARN.' );
        
        $logger.setLevel($logger.ERROR);
        equals($logger.getLevel(), $logger.ERROR, 'Set level to ERROR and get level returns ERROR.' );
        
        $logger.setLevel($logger.FATAL);
        equals($logger.getLevel(), $logger.FATAL, 'Set level to FATAL and get level returns FATAL.' );
    });

    test('$logger.setAppender() and $logger.getAppender()', function() {
        expect(1);
        
        $logger.reset();
        
        var fakeAppender = function() {
            return {
                'append' : function() {
                    return Number.MAX_VALUE;
                }
            };
        };
        $logger.setAppender(fakeAppender());
        var appender = $logger.getAppender();
        ok((appender.append() == Number.MAX_VALUE), 'Appender is set correctly.');
    });

    test('$logger.setOptions() and $logger.getOptions()', function() {
        expect(2);
        
        $logger.reset();
        var fakeAppender = function() {
            return {
                'append' : function() {
                    return Number.MAX_VALUE;
                }
            };
        };
        $logger.setOptions({
            'level' : $logger.ERROR,
            'appender' : fakeAppender()
        });
        var options = $logger.getOptions();
        ok((options.level == $logger.ERROR), 'Level is set to ERROR.' );
        ok((options.appender.append() == Number.MAX_VALUE), 'Appender is set correctly.');
    });
    
    test('$logger.getLogger()', function() {
        ok(!$logger.getLogger, 'Child logger instance does not have getLogger() function.');
    });
    
    test('Interference', function() {
        $.jql.reset();
        $logger.reset();
        var $logger2 = $.jql.getLogger('secondLogger');
        
        $.jql.setLevel($.jql.ERROR);
        $logger.setLevel($logger.WARN);
        $logger2.setLevel($logger.TRACE);
        
        equals($.jql.getLevel(), $.jql.ERROR, '$.jql instance log level is set to ERROR.');
        equals($logger.getLevel(), $logger.WARN, '$logger instance log level is set to WARN.');
        equals($logger2.getLevel(), $logger2.TRACE, '$logger2 instance log level is set to TRACE.');
    });
});
</script>
</head>
<body>
    <h1 id="qunit-header">$.jql unit test</h1>
    <h2 id="qunit-banner"></h2>
    <div id="qunit-testrunner-toolbar"></div>
    <h2 id="qunit-userAgent"></h2>
    <ol id="qunit-tests"></ol>
</body>
</html>